package com.lsf.modules.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lsf.common.api.vo.Result;
import com.lsf.modules.system.entity.Question;
import com.lsf.modules.system.entity.Reply;
import com.lsf.modules.system.entity.User;
import com.lsf.modules.system.service.CommentService;
import com.lsf.modules.system.service.QuestionService;
import com.lsf.modules.system.service.ReplyService;
import com.lsf.modules.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author Alexander
 */
@RestController
@Slf4j
@RequestMapping("/sys/reply")
public class ReplyController {

    @Resource
    private ReplyService replyService;

    @Resource
    private UserService userService;

    @Resource
    private QuestionService questionService;

    @Resource
    private CommentService commentService;

    @RequestMapping("/add")
    public Result<JSONObject> add(@RequestBody @Validated Reply reply) {
        Result<JSONObject> result = new Result<>();
        User user = userService.getById(reply.getAuthorId());
        if (user == null) {
            result.error500("作者不存在!");
            return result;
        }
        Question question = questionService.getById(reply.getQuestionId());
        if (question == null) {
            result.error500("提问不存在!");
            return result;
        }
        LambdaQueryWrapper<Reply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Reply::getAuthorId, reply.getAuthorId());
        queryWrapper.eq(Reply::getQuestionId, reply.getQuestionId());
        Reply one = replyService.getOne(queryWrapper);
        if (one != null) {
            result.error500("您已经完成过回帖,不可重复添加,可以选择修改回帖内容!");
            return result;
        }
        reply.setCreateTime(new Date());
        boolean save = replyService.save(reply);
        if (!save) {
            result.error500("添加失败!");
        }
        result.success("添加成功!");
        return result;
    }

    @PostMapping("/edit/{id}")
    public Result edit(@PathVariable String id, @RequestParam("content") String content) {
        Result result = new Result();
        Reply reply = replyService.getById(id);
        if (reply == null) {
            result.error500("无此回帖!");
            return result;
        }
        if (StringUtils.isEmpty(content)) {
            result.error500("回帖内容为空!");
            return result;
        }
        reply.setContent(content);
        boolean update = replyService.updateById(reply);
        if (!update) {
            result.error500("修改失败!");
            return result;
        }
        result.setSuccess(true);
        return result;
    }

    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        Result result = new Result();
        Reply reply = replyService.getById(id);
        if (reply == null) {
            result.error500("无此回帖!");
            return result;
        }
        boolean remove = replyService.removeById(id);
        if (!remove) {
            result.error500("删除失败!");
            return result;
        }
        result.setSuccess(true);
        return result;
    }

    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable String id) {
        Result result = new Result();
        Reply reply = replyService.getById(id);
        Question question = questionService.getById(reply.getQuestionId());
        User user = userService.getById(reply.getAuthorId());
        result.setResult(user);
        result.setResult(question);
        result.setResult(reply);
        result.setSuccess(true);
        return result;
    }

    @GetMapping("/vote/{id}")
    public Result vote(@PathVariable String id, @RequestParam("username") String username) {
        Result result = new Result();
        Reply reply = replyService.getById(id);
        if (reply == null) {
            result.error500("无此回帖!");
            return result;
        }
        User user = userService.getUserByName(username);
        if (user == null) {
            result.error500("用户不存在!");
            return result;
        }
        int i = replyService.vote(reply, user);
        result.setResult(i);
        result.setSuccess(true);
        return result;
    }

}
